import RandomUtil from '@/RandomUtil'
import ArrayUtil from '@/ArrayUtil'
import { bossReward } from './config/bossReward'
import { d_shop_items } from 'xlsx/shop_items'
import UnitCommon from 'code/Core/Common/UnitCommon'
import MovementCommon from 'code/Core/Common/MovementCommon'
import { d_sp_items } from 'xlsx/sp_items'

export default class MonsterReward {
    static dropList: string[][] = []
    static 额外掉落表: { id: string; rate: number }[] = []
    static 技能书id表: string[] = []
    static 洗炼石表: string[] = []
    constructor() {
        this.init()
        EventManager.单位死亡(MonsterReward.单位死亡)
    }
    init() {
        let temp: ShopItemModel[] = []
        let bossDrop: ShopItemModel[] = []
        for (let cfg of d_shop_items) {
            if (cfg.CanBuy == 0) {
                bossDrop.push(cfg)
                MonsterReward.额外掉落表.push({
                    id: cfg.id,
                    rate: cfg.goldcost,
                })
            } else {
                if (!cfg.Name.includes('(卷轴)')) temp.push(cfg)
            }
        }

        // 根据价格排序
        MonsterReward.额外掉落表.sort((prev, next) => prev.rate - next.rate)
        // 交换权重
        for (let i = 0; i < MonsterReward.额外掉落表.length / 2; i++) {
            let temp = MonsterReward.额外掉落表[i].rate
            MonsterReward.额外掉落表[i].rate =
                MonsterReward.额外掉落表[
                    MonsterReward.额外掉落表.length - 1 - i
                ].rate
            MonsterReward.额外掉落表[
                MonsterReward.额外掉落表.length - 1 - i
            ].rate = temp
        }
        // 计算总权重
        let totolPrice = 0
        for (let item of MonsterReward.额外掉落表) {
            totolPrice += item.rate
        }
        for (let i = 0; i < MonsterReward.额外掉落表.length; i++) {
            let item = MonsterReward.额外掉落表[i]
            // 以小数来计算权重
            item.rate = item.rate / totolPrice
        }

        // 洗炼石
        MonsterReward.洗炼石表 = d_sp_items
            .filter((item) => item.Name.includes('洗炼石'))
            .map((item) => item.id)
        for (let i = 0; i < 5; i++) {
            // 增加普通洗炼石的掉落机率
            MonsterReward.洗炼石表.push('Jxls')
        }

        MonsterReward.dropList[0] = []
        MonsterReward.dropList[1] = temp
            .filter((item) => item.goldcost < 200)
            .map(MonsterReward.mapItemId)
        MonsterReward.dropList[2] = temp
            .filter((item) => item.goldcost < 400)
            .map(MonsterReward.mapItemId)
        MonsterReward.dropList[3] = temp
            .filter((item) => item.goldcost < 600 && item.goldcost > 200)
            .map(MonsterReward.mapItemId)
        MonsterReward.dropList[4] = temp
            .filter((item) => item.goldcost < 800 && item.goldcost > 200)
            .map(MonsterReward.mapItemId)
        // boss
        MonsterReward.dropList[5] = [].concat(
            bossDrop
                .filter((item) => item.goldcost < 2000 && item.goldcost > 200)
                .map(MonsterReward.mapItemId)
        )
        //
        MonsterReward.dropList[6] = temp
            .filter((item) => item.goldcost < 1000 && item.goldcost > 400)
            .map(MonsterReward.mapItemId)
        MonsterReward.dropList[7] = temp
            .filter((item) => item.goldcost < 1200 && item.goldcost > 400)
            .map(MonsterReward.mapItemId)
        MonsterReward.dropList[8] = temp
            .filter((item) => item.goldcost < 1400 && item.goldcost > 600)
            .map(MonsterReward.mapItemId)
        MonsterReward.dropList[9] = temp
            .filter((item) => item.goldcost < 1600 && item.goldcost > 600)
            .map(MonsterReward.mapItemId)
        // boss
        MonsterReward.dropList[10] = [].concat(
            bossDrop
                .filter((item) => item.goldcost < 3000 && item.goldcost > 1500)
                .map(MonsterReward.mapItemId)
        )
        //
        MonsterReward.dropList[11] = temp
            .filter((item) => item.goldcost < 1800 && item.goldcost > 800)
            .map(MonsterReward.mapItemId)
        MonsterReward.dropList[12] = temp
            .filter((item) => item.goldcost < 2000 && item.goldcost > 1000)
            .map(MonsterReward.mapItemId)
        MonsterReward.dropList[13] = temp
            .filter((item) => item.goldcost < 2200 && item.goldcost > 1000)
            .map(MonsterReward.mapItemId)
        MonsterReward.dropList[14] = temp
            .filter((item) => item.goldcost < 2400 && item.goldcost > 1200)
            .map(MonsterReward.mapItemId)
        // boss
        MonsterReward.dropList[15] = [].concat(
            bossDrop
                .filter((item) => item.goldcost < 4000 && item.goldcost > 2000)
                .map(MonsterReward.mapItemId)
        )
        //
        MonsterReward.dropList[16] = temp
            .filter((item) => item.goldcost < 2600 && item.goldcost > 1200)
            .map(MonsterReward.mapItemId)
        MonsterReward.dropList[17] = temp
            .filter((item) => item.goldcost < 2800 && item.goldcost > 1400)
            .map(MonsterReward.mapItemId)
        MonsterReward.dropList[18] = temp
            .filter((item) => item.goldcost < 3000 && item.goldcost > 1400)
            .map(MonsterReward.mapItemId)
        MonsterReward.dropList[19] = temp
            .filter((item) => item.goldcost < 3200 && item.goldcost > 1600)
            .map(MonsterReward.mapItemId)
        // boss
        MonsterReward.dropList[20] = [].concat(
            bossDrop
                .filter((item) => item.goldcost > 3000)
                .map(MonsterReward.mapItemId)
        )

        // 技能书
        MonsterReward.技能书id表 = GlobalDB.ItemDB.allSkillIds
    }
    static mapItemId(v: any): string {
        return v.id
    }
    static 单位死亡(d: DamageEventData) {
        MonsterReward.普通掉落(d)
        MonsterReward.随机额外掉落(d.target)
        MonsterReward.技能书掉落(d)
        MonsterReward.洗炼石掉落(d)
    }
    static 洗炼石掉落(d: DamageEventData) {
        let r = RandomUtil.randomPercent()
        let isBoss = UnitCommon.是否精英单位(d.target)
        let max = isBoss ? 0.5 : 0.05
        let c = isBoss ? RandomUtil.nextInt(1, 3) : 1
        if (r < max) {
            for (let i = 0; i < c; i++) {
                let id = ArrayUtil.randomElement(MonsterReward.洗炼石表)
                CreateItem(id, GetUnitX(d.target), GetUnitY(d.target))
            }
        }
    }
    static 技能书掉落(d: DamageEventData) {
        let r = RandomUtil.randomPercent()
        let isBoss = UnitCommon.是否精英单位(d.target)
        let max = isBoss ? 0.5 : 0.035
        let c = isBoss ? RandomUtil.nextInt(1, 3) : 1
        if (r < max) {
            for (let i = 0; i < c; i++) {
                let id = ArrayUtil.randomElement(MonsterReward.技能书id表)
                CreateItem(id, GetUnitX(d.target), GetUnitY(d.target))
            }
        }
    }
    static 普通掉落(d: DamageEventData) {
        let w = game_wave
        let dr = RandomUtil.randomPercent()
        let list = MonsterReward.dropList[w]
        if (w % 5 != 0) {
            if (dr < 0.2) {
                let id = ArrayUtil.randomElement(list)
                if (id) {
                    let r = RandomUtil.randomPercent()
                    if (r < 0.6) {
                        let attr = AbilityIncreaseInstence.increasReward()
                        MovementCommon.dropItemFromUnit(id, d.target, attr)
                    } else {
                        MovementCommon.dropItemFromUnit(id, d.target)
                    }
                }
            }
        }
        if (!UnitCommon.是否精英单位(d.target)) return
        //  boss 必定掉落
        let c = 0
        if (dr < 0.5) {
            c = 1
        } else if (dr < 0.85) {
            c = 2
        } else {
            c = 3
        }
        for (let i = 0; i < c; i++) {
            let id = ArrayUtil.randomElement(list)
            let attr = AbilityIncreaseInstence.increasReward()
            MovementCommon.dropItemFromUnit(id, d.target, attr)
        }
        MonsterReward.boss掉落(d.target)
    }
    static boss掉落(u: unit) {
        let id = id2string(GetUnitTypeId(u))
        let list = bossReward[id]
        if (list?.length == 0) return
        let r2 = RandomUtil.randomPercent()
        let c = 1
        if (r2 > 0.5) {
            c = 2
        }
        for (let i = 0; i < c; i++) {
            let itemId = ArrayUtil.randomElement(list)
            let attr = AbilityIncreaseInstence.increasReward()
            MovementCommon.dropItemFromUnit(itemId, u, attr)
        }
    }
    static 随机额外掉落(u: unit) {
        let r = RandomUtil.randomPercent()
        if (r < 0.02) {
            r = RandomUtil.randomPercent()
            for (let item of MonsterReward.额外掉落表) {
                r -= item.rate
                if (r <= 0) {
                    let attr = AbilityIncreaseInstence.increasReward()
                    MovementCommon.dropItemFromUnit(item.id, u, attr)
                    break
                }
            }
        }
    }
}
